package com.wly.control
{
	import com.wly.control.common.BaseControl;
	import com.wly.datapool.DataPool;
	import com.wly.event.EventCodes;
	import com.wly.event.EventManager;
	import com.wly.ui.PlayerInfo;
	
	import fl.motion.motion_internal;
	
	public class PlayerInfoControl extends BaseControl
	{
		public function PlayerInfoControl()
		{
			super();
		}
		
		override protected function onUIAttach():void
		{
			super.onUIAttach();
			
			EventManager.getInstance().registerEvent(EventCodes.EVENT_PLAYER_SCORE_UPDATE, updatePlayerScoreInfo);
			EventManager.getInstance().registerEvent(EventCodes.EVENT_PLAYER_LIFE_UPDATE, updatePlayerLifeInfo);
			EventManager.getInstance().registerEvent(EventCodes.EVENT_PLAYER_RMB_UPDATE, updatePlayerRmbInfo);
		}
		
		override protected function onUIDeattach():void
		{
			EventManager.getInstance().unRegisterEvent(EventCodes.EVENT_PLAYER_SCORE_UPDATE, updatePlayerScoreInfo);
			EventManager.getInstance().unRegisterEvent(EventCodes.EVENT_PLAYER_LIFE_UPDATE, updatePlayerLifeInfo);
			EventManager.getInstance().unRegisterEvent(EventCodes.EVENT_PLAYER_RMB_UPDATE, updatePlayerRmbInfo);
			
			super.onUIDeattach();
		}
		
		protected function get playerInfo():PlayerInfo
		{
			return _ui as PlayerInfo;
		}
		
		private function updatePlayerScoreInfo(param:*=null):void
		{
			if(playerInfo)
			{
				playerInfo.playerScore = DataPool.getInstance().mainPlayer.score;
			}
		}
		
		private function updatePlayerLifeInfo(param:*=null):void
		{
			if(playerInfo)
			{
				playerInfo.playerLifeNum = DataPool.getInstance().mainPlayer.lifeNum;
			}
		}
		
		private function updatePlayerRmbInfo(param:*=null):void
		{
			if(playerInfo)
			{
				playerInfo.rmb = DataPool.getInstance().mainPlayer.rmb;
			}
		}
		
		override public function updateUI():void
		{
			updatePlayerScoreInfo();
			updatePlayerLifeInfo();
			updatePlayerRmbInfo();
		}
	}
}